using GalaSoft.MvvmLight;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using System.Windows;
using GalaSoft.MvvmLight.Messaging;
using System;

namespace Arcanum.Game.Client.Presentation.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        public const string HAS_CONSOLE_PROPERTY_NAME = "HasConsole";

        private bool hasConsole;

        public bool HasConsole
        {
            get { return hasConsole; }
            set
            {
                if (hasConsole != value)
                {
                    hasConsole = value;
                    this.RaisePropertyChanged(HAS_CONSOLE_PROPERTY_NAME);
                }
            }
        }

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            this.ExitCommand = new RelayCommand(this.Exit);
            this.ToggleConsoleCommand = new RelayCommand<MainWindow>(this.ToggleConsole);
            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real"
            ////}
        }

        public Visibility ConsoleVisibility
        {
            get
            {
                if (this.HasConsole) return Visibility.Visible;
                else return Visibility.Hidden;
            }
        }

        public ICommand ExitCommand { get; set; }

        public ICommand ToggleConsoleCommand { get; set; }

        private void Exit()
        {
            Application.Current.MainWindow.Close();
        }

        private void ToggleConsole(MainWindow mainWindow) 
        {
            if (this.HasConsole)
            {
                this.CloseConsole();
                Console.WriteLine("Console closed.");
            }
            else
            {
                this.OpenConsole();
                Console.WriteLine("Console opened.");
            }
            mainWindow.consoleTab.Visibility = this.ConsoleVisibility;
        }

        private void OpenConsole()
        {
            this.HasConsole = true;
        }

        private void CloseConsole()
        {
            this.HasConsole = false;
        }
    }
}